import { IFeidaoAiBrowserComponent } from '@dfeidao/atom-web/interfaces';
import get from '@dfeidao/atom-web/local/get';
// import remove from '@dfeidao/atom-web/local/remove';
import javadsd from '@dfeidao/atom-web/msg/javadsd';
import nodejs from '@dfeidao/atom-web/msg/nodejs';
// import whoami from '@dfeidao/atom-web/msg/whoami';
import confirm, { ConfirmResult } from '@dfeidao/atom-web/msgbox/confirm';
import error from '@dfeidao/atom-web/msgbox/error';
import success from '@dfeidao/atom-web/msgbox/success';
import tree from '@dfeidao/fd-w000001';
export default async function a012(fd: IFeidaoAiBrowserComponent) {
	// 保存
	const productid = get(fd, 'productid');
	const level_no = get(fd, 'level_no111');
	// debugger;
	const listt = fd.data.node.querySelectorAll<HTMLDivElement>('[draggable="true"]');
	const atomm = new Array();
	if (atomm.length > 0) {
		listt.forEach((i: HTMLElement) => {
			const level_no1 = i.getAttribute('level_no');
			const level_name1 = i.getAttribute('level_name');
			const order_num1 = i.getAttribute('order_num');
			atomm.push({ level_no: level_no1, level_name: level_name1, order_num: order_num1 });
		});
		await nodejs('level-inf/zj-000001/s006', { atomm });
	}
	if (!level_no) {
		error('请先选择等级');
		return;
	}
	const select = fd.data.node.querySelector<HTMLSelectElement>('[select]');
	const index = select.options.selectedIndex;
	const client_type1 = select.options[index].value;
	const zds = fd.data.node.querySelector('[zds]') as tree;
	const checked = zds.get_checked() as [{ no: string, formname: string, formid: string, action_keyword: string, action_name: string, code: string }];
	const att = new Array();
	checked.forEach((i) => {
		if (i.no && att.indexOf(i.no) === -1) {
			att.push(i.no);
		}
	});
	const arr = att.map((y) => {
		return zds.get_data_by_id(y);
	});
	arr.forEach((i) => {
		checked.push(i);
	});
	// const rey = new Array();
	// checked.forEach((item) => {
	// 	rey.push(item.no);
	// });
	// zds.check_node(rey);
	// checked.forEach((i))
	const resultarr = new Array();
	for (let i = 0; i < checked.length; i++) {
		if (resultarr.indexOf(checked[i]) === -1) {
			resultarr.push(checked[i]);
		}
	}
	const zjd = new Array();
	const fjd = new Array();
	resultarr.forEach((i) => {
		if (!i.no) {
			const formid = i.formid;
			const formname = i.formname;
			const client_type = client_type1;
			fjd.push({ formid, formname, client_type, productid, level_no });
		} else {
			const formid = i.no;
			const action_keyword = i.action_keyword;
			const action_name = i.action_name;
			const code = i.code;
			zjd.push({ formid, action_keyword, action_name, code, productid, level_no });
		}
	});
	const flag = await confirm('是否更新该等级下租户已分配的功能？', '提示', '确定', '取消', 'rgba(153, 153, 153, 1)');
	switch (flag) {
		case ConfirmResult.YES:
			// 确定
			const res1 = await nodejs('level-inf/zj-000001/s007', {
				productid, level_no, zjd
				, fjd, client_type: client_type1
			});
			const condition = {
				param: {
					level_no,
					productid
				}
			};
			const ret = await javadsd('12', '12', 'levelformchangetenantform', condition);
			window.console.log(res1, ret);
			if (res1 === 'success') {
				success('成功');
				const rey = new Array();
				checked.forEach((item) => {
					rey.push(item.no);
				});
				zds.check_node(rey);
				// remove(fd, 'level_no111');
			} else {
				error('失败');
			}
			break;
		case 'no':
		// 取消
		// 	const res = await nodejs('level-inf/zj-000001/s007', { productid, level_no, zjd, fjd });
		// 	window.console.log(res);
		// 	if (res === 'success') {
		// 		success('成功');
		// 		// remove(fd, 'level_no111');
		// 	} else {
		// 		error('失败');
		// 	}
		// 	break;
		// default:  // cancel
		// 	// todo
		// 	break;
	}
}
